package com.base.mail;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.bean.StatEntity;
import com.base.commonlib.utils.RxBus2;
import com.base.mail.adapter.BaseAdapter;
import com.base.mail.adapter.BaseHolder;
import com.base.mail.adapter.ContactAdapter;
import com.base.mail.bean.ContactEntity;
import com.base.mail.databinding.FragmentConstractBinding;
import com.base.mail.presenter.ConstratPresenter;
import com.base.mail.utils.LetterView;
import com.base.mail.view.ConstractView;

import java.util.List;

/**
 * 通讯录
 * Created by weikailiang on 2019/9/19.
 */
public class ConstractFragment extends CBaseMvpFragment<ConstratPresenter> implements ConstractView {
    protected FragmentConstractBinding mBinding;
    private TextView mTitle;

    private LinearLayoutManager mLinearLayoutManager;
    private ContactAdapter mAdapter;
    private Runnable mOverlay_gone;
    private Handler mHandler=new Handler();
    @Override
    public void init() {

        mBinding = (FragmentConstractBinding) mRootBinding;
        mTitle = mBinding.getRoot().findViewById(R.id.title);
        mTitle.setText("通讯录");
        mBinding.imgRight.setImageResource(R.drawable.ic_contact_tree);
        mBinding.imgRight.setVisibility(View.VISIBLE);
        mBinding.imgRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                ARouter.getInstance().build("/mail/organ").navigation();
            }
        });
    }

    @Override
    public void start() {
        RxBus2.getInstance().post(new StatEntity(1));
        mPresenter = new ConstratPresenter(this,_mActivity);
        mBinding.llReload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //树状机构
//                mPresenter.getContacts();
            }
        });
        mBinding.tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mBinding.etSearch.setText("");
            }
        });

//        mPresenter.getContacts();
        mOverlay_gone = new Runnable() {

            @Override
            public void run() {
                mBinding.tvOverlay.setVisibility(View.GONE);
            }
        };

        mBinding.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = s.toString();
                mPresenter.doSearch(keyword);
                if(TextUtils.isEmpty(keyword))
                {
                    mBinding.tvCancel.setVisibility(View.GONE);
                }else {
                    mBinding.tvCancel.setVisibility(View.VISIBLE);
                }
            }
        });
        mBinding.letter.setOnLetterSelectedListener(new LetterView.LetterSelectedListener() {
            @Override
            public void onLetterSelectedListener(String letter) {
                if(mAdapter!=null&&mAdapter.mLetterIndexer!=null)
                {
                    int section = mAdapter.mLetterIndexer.getSectionForItem(letter);
                    if(section>=0)
                    {
                        int position = mAdapter.mLetterIndexer.getPositionForSection(section);
                        if(position>=0)
                        {
                            int firstP = mLinearLayoutManager.findFirstVisibleItemPosition();
                            int lastP = mLinearLayoutManager.findLastVisibleItemPosition();
                            if(position>=firstP&&position<=lastP)
                            {
                                View childView = mBinding.rv.getChildAt(position - firstP);
                                mBinding.rv.scrollBy(0,childView.getTop());
                            }else
                            {
                                mLinearLayoutManager.scrollToPosition(position);
                            }

                        }
                    }
                }

                mHandler.removeCallbacks(mOverlay_gone);
                mBinding.tvOverlay.setVisibility(View.VISIBLE);
                mBinding.tvOverlay.setText(letter);
                mHandler.postDelayed(mOverlay_gone,1500);
            }
        });
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_constract;
    }

    @Override
    public void setData(List<ContactEntity> data) {
        mLinearLayoutManager = new LinearLayoutManager(mContext);
        mAdapter = new ContactAdapter(data, mContext);
        mAdapter.putNormalType(R.layout.item_contact);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                ARouter.getInstance().build("/mail/maildetail")
                        .withString("userId",mPresenter.mData.get(dataPosition).getUserid()).navigation();
            }
        });
        mBinding.rv.setLayoutManager(mLinearLayoutManager);
        mBinding.rv.setAdapter(mAdapter);
    }

    @Override
    public void showSearch(List<ContactEntity> data) {
        if(mAdapter!=null)
        {
            mAdapter.setData(data);
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (getView() != null) {
            getView().setVisibility(isVisibleToUser ? View.VISIBLE : View.INVISIBLE);
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden){
            RxBus2.getInstance().post(new StatEntity(1));
        }
    }

    @Override
    public void showReload() {
        mBinding.llReload.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideReload() {
        mBinding.llReload.setVisibility(View.GONE);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.release();
    }
}
